// pages/luoAn/common/loginIndex.js
const App = getApp()
const {
  LuoAnHttp
} = App.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.wxCodeGet();
  },

  wxCodeGet: async function () {
    const code1 = await LuoAnHttp.asynWXLogin({});
    console.log('code', code1)
    return code1
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  popMethod: function (e) {
    let pages = getCurrentPages();
    if (pages.length > 1) {
      wx.navigateBack();
    } else {
      wx.reLaunch({
        url: '/pages/luoAn/device/deviceIndex',
      })
    }
  },
  serviceDetail: function (e) {
    wx.navigateTo({
      url: '/pages/luoAn/my/about/serviceProtocol',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindgetuserinfo: function (e) {
    console.log('bindgetuserinfo', e);
    if (e.detail.userInfo) {
      this.setData({
        userInfo: e.detail.userInfo,
        checked: !this.data.checked
      })
    }
  },
  wxCheckSession: function () {
    return new Promise((resolve, reject) => {
      wx.checkSession({
        success: (res) => {
          console.log(res);
          return resolve(true)
        },
        fail: f => {
          console.log(f);
          return resolve(false)
        }
      })
    })
  },

  bindgetphonenumber: async function (e) {
    const evn = this.getOpenerEventChannel()
    console.log(e);
     
    const detail = e.detail;
    if (detail && detail.encryptedData) {
      const code1 = await this.wxCodeGet();
      const {
        encryptedData,
        iv
      } = detail
      const res = await LuoAnHttp.asyncCommonDecode({
        encryptedData,
        iv,
        code: code1
      });

      if (res && res.data) {
        const mobile = res.data.phoneNumber
        const code2 = await this.wxCodeGet();
        const openIdData = await LuoAnHttp.asyncCommonGetOpenId({
          code: code2
        })
        if (openIdData && openIdData.data) {
          const loginRes = await LuoAnHttp.asyncLogin({
            mobile,
            openid: openIdData.data.openid,
            nickname: this.data.userInfo.nickName,
            userProvince: this.data.userInfo.province,
            userCity: this.data.userInfo.city,
            headportraitUrl: this.data.userInfo.avatarUrl
          });
          if (loginRes && loginRes.code == 200) {
            const userIdData = await LuoAnHttp.asyncGetUserId({
              phone: mobile
            })
            if (userIdData.data&&userIdData.data.userid) {
              wx.setStorageSync('userId', userIdData.data.userid)
               
              evn && evn.emit('callback')
              wx.reLaunch({
                url: '/pages/luoAn/device/deviceIndex',
              })
              // this.setData({
              //   userId:userIdData.data.userid
              // })
              // this.loadDeviceList({pagesize:10,pagecurrent:1})
            }
          }
        }
      }
    }
  },
})